/*
 License
 
 Copyright © 2012 Kevin Kimmich
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights to 
 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 the Software, and to permit persons to whom the Software is furnished to do so, 
 subject to the following conditions:
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 @file
 A whole bunch of types used in the core library.
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdint.h>
#include <memory.h>
#include <errno.h>
#include <pthread.h>
#include <setjmp.h>
#include <assert.h>
#include <stdarg.h>
#ifdef ENABLE_LIBXML
#include <libxml/parser.h>
#include <libxml/xpath.h>
#endif
#include <zlib.h>
#ifdef ENABLE_SSL
#include <openssl/ssl.h>
#endif
#include <dirent.h>

#ifndef _crtypes_h
#define _crtypes_h

#ifdef __cplusplus
extern "C" {
#endif


#define LOCAL_BUF_SIZE (4096)

/*!
 An item in a linked list. This should be the first element in a struct that will
 be kept in a list.
 @param next Pointer to the next item in the list.
 @param prev Pointer to the previous item in the list.
 @param list The list owner of the node (do not edit)
 */
typedef struct list_node_t
{
	struct	list_node_t* next;
	struct	list_node_t* prev;
	struct list_t* list;
} list_node_t;

/*!
 A doubly linked list.
 @param head An anchor to the list.
 @param count The number of elements in the list.
 @param mutex A mutex for the list.
 */
typedef struct list_t
{
	list_node_t head;
	int count;
	pthread_mutex_t mutex;
} list_t;

/*!
 A private data structure for hash_string_to_pointer.c. DEPRECATED!
 */
typedef struct hash_string_node_t 
{
	struct hash_string_node_t *next;
	void* data;
	char * key;
} hash_string_node_t;

/*!
 The hash string to pointer table structure. DEPRECATED!
 */
typedef struct hash_string_t 
{
	struct hash_string_node_t **bucket;
	int size;
	int entries;
	int downshift;
	int mask;
	pthread_mutex_t mutex;
} hash_string_t;

/*!
 Information about a memory mapped file.
 @param st The struct stat containing information about the file size, date, etc...
 @param fd The file descriptor of the file backing the memory region.
 @param data The pointer to the first byte of the memory region (first byte of the file)
 @param offset (Not used)
 @param length The length of the file.
 @param filename The name of the file.
 @param no_swap Zero is not mlocked, 1 if mlocked.
 */
typedef struct mapped_file_t
{
	struct stat st;
	int fd;
	char* data;
	off_t offset;
	size_t length;
	char* filename;
	int no_swap;
} mapped_file_t;

/*!
 A private structure for core_thread()
 */
typedef struct
{
	pthread_t id;
	int detached;
	int argc;
	void** argv;
	void*(*func)(int argc, void** argv);
} core_thread_t;

/*!
 An HTTP result code structure
 */
typedef struct result_code
{
	unsigned int code;
	const char* information_string;
} result_code;


#ifndef __cplusplus
typedef	uint32_t bool;
#endif

#ifndef TRUE
#define TRUE (1)
#endif
#ifndef FALSE
#define FALSE (0)
#endif

/*!
 A structure for measuring durations.
 Initialize it as follows:
 elapso e = ELAPSO_INIT;
 */
typedef struct elapso
{
	struct timeval start;
	struct timeval stop;
	void(*start_me)(struct elapso*);
	double(*stop_me)(struct elapso*);
} elapso;

#define ELAPSO_INIT {{0,0},{0,0},__start_me,__stop_me};


#include "stream.h"

typedef struct stringbuffer
{
	struct stream s;
	char* data;//do not use this directly!
} stringbuffer;

typedef stringbuffer text;

typedef struct
{
	pthread_mutex_t mutex;
	float* buffer;
	float* head;
	float* tail;
	int sample_count;
	int buffer_size;
} circle_buffer_t;

/*!
 The callback function for iterate_file().
 @param obj private pointer.
 @param count pointer to the count integer.
 @param is_file Is the item a file?
 @param is_dir Is the item a directory?
 @param full_path The full path to the file.
 @param filename The name of the file.
 @param stat The stat structure for the file.
 */
typedef void(*iterate_file_callback)(void* obj, int* count, int is_file, int is_dir, char* full_path, char* filename, struct stat* stat);

#ifdef __cplusplus
}
#endif

	
#endif
